//
//  SignMainView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/26.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SignMainView.h"
#import "SignListCell.h"
#import "SignMianBGView.h"

@interface SignMainView()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    CGFloat _selfWidth;
    CGFloat _selfHeight;
}

@property (nonatomic, strong) UIView *rightView;
@property (nonatomic, strong) UILabel *title_label;
@property (nonatomic, strong) UIButton *sign_button;
@property (nonatomic, strong) UITextView *desc_text_view;
@property (nonatomic, strong) UITextView *word_text_view;

@property (nonatomic, strong) UICollectionView *sign_list_view;
@property (nonatomic, strong) UIImageView *gift_image_view;

@end

@implementation SignMainView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _selfWidth = SCREEN_WIDTH - 60;
        _selfHeight = SCREEN_HEIGHT - 100;
        
        
        [self makeRightView];
        [self makeLeftView];
        
    }
    return self;
}

- (void)makeRightView
{
    CGFloat rightWidth = (_selfWidth) / 3;
    
    _rightView = [UIView new];
    [self addSubview:_rightView];
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(0);
        make.width.mas_equalTo(rightWidth);
    }];
    
    CGFloat buttonHeight = (rightWidth) / 2 * 7 / 19;
    _sign_button = [UIButton new];
    [_sign_button setImage:[UIImage imageNamed:@"sign_button_image"] forState:UIControlStateNormal];
    [_sign_button addTarget:self action:@selector(clickSignInButton:) forControlEvents:UIControlEventTouchUpInside];
    [_rightView addSubview:_sign_button];
    [_sign_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((rightWidth) / 2, buttonHeight));
        make.centerX.mas_equalTo(_rightView);
        make.bottom.mas_equalTo(-10);
    }];
    
    CGFloat moduleHeight = ((_selfHeight) - (buttonHeight) - 20) / 3;
    
    _title_label = [UILabel new];
    _title_label.font = [UIFont systemFontOfSize:25.0f];
    _title_label.textAlignment = NSTextAlignmentCenter;
    _title_label.numberOfLines = 0;
    [_rightView addSubview:_title_label];
    [_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(moduleHeight);
    }];
    
    _desc_text_view = [UITextView new];
    _desc_text_view.editable = NO;
    _desc_text_view.font = [UIFont systemFontOfSize:15.0f];
    [_rightView addSubview:_desc_text_view];
    [_desc_text_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_title_label.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(moduleHeight);
    }];
    
    _word_text_view = [UITextView new];
    _word_text_view.editable = NO;
    _word_text_view.font = [UIFont systemFontOfSize:15.0f];
    [_rightView addSubview:_word_text_view];
    [_word_text_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_desc_text_view.mas_bottom).mas_offset(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(moduleHeight);
    }];
}

- (void)loadTaskInfo:(TaskModel *)taskInfo
{
    _title_label.text = taskInfo.name;
    
    _desc_text_view.text = [NSString stringWithFormat:@"        %@",taskInfo.descriptionText];
    
    _word_text_view.text = [NSString stringWithFormat:@"        %@",taskInfo.sendwordText];
    
    [_gift_image_view sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseIp,taskInfo.image]]];
}

- (void)makeLeftView
{
    UIView *leftView = [UIView new];
    leftView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    leftView.layer.shadowOffset = CGSizeMake(3,3);//shadowOffset阴影偏移,x向右偏移2，y向下偏移6，默认(0, -3),这个跟shadowRadius配合使用
    leftView.layer.shadowOpacity = 0.6;//阴影透明度，默认0       _imageView.layer.shadowRadius = 2;//阴影半径，默认3
    leftView.layer.shadowColor = [RGB(168, 168, 168) CGColor];
    [self addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(_rightView.mas_left).mas_offset(0);
    }];
    
    
    CGFloat leftWidth = _selfWidth - (_selfWidth) / 3;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0.5;
    flowLayout.minimumInteritemSpacing = 0.5;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake((leftWidth - 3.5) / 8, (_selfHeight - 1.5) / 4);
    
    _sign_list_view = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _sign_list_view.dataSource = self;
    _sign_list_view.delegate = self;
    [_sign_list_view registerClass:[SignListCell class] forCellWithReuseIdentifier:NSStringFromClass([SignListCell class])];
    _sign_list_view.layer.masksToBounds = YES;
    _sign_list_view.layer.borderColor = RGB(240, 240, 240).CGColor;//shadowColor阴影颜色
    _sign_list_view.layer.borderWidth = 1.0;
    SignMianBGView *bgView = [[SignMianBGView alloc] initWithFrame:CGRectMake(0, 0, leftWidth, _selfHeight)];
    _sign_list_view.backgroundView = bgView;
    
    [leftView addSubview:_sign_list_view];
    [_sign_list_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    
    _gift_image_view = [UIImageView new];
    _gift_image_view.layer.masksToBounds = YES;
    _gift_image_view.layer.borderWidth = 0;
    _gift_image_view.contentMode = UIViewContentModeScaleAspectFill;
    [leftView addSubview:_gift_image_view];
    [_gift_image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake((leftWidth - 7) / 8, (_selfHeight - 3) / 4));
        make.bottom.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
}

- (void)setSignData:(NSArray *)signData
{
    _signData = signData;
    [_sign_list_view reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _signData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SignListCell *listCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SignListCell class]) forIndexPath:indexPath];
    
    [listCell loadSignData:(SignModel *)[_signData objectAtIndex:indexPath.row]];
    
    return listCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(didSelectSignModel:)]) {
        [self.delegate didSelectSignModel:(SignModel *)[_signData objectAtIndex:indexPath.row]];
    }
}



- (void)clickSignInButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickSignIn)]) {
        [self.delegate clickSignIn];
    }
}

@end
